package cn.zhl.algorithm._3_并查集.impl;

import cn.zhl.algorithm._3_并查集.UnionFind;

/**
 * @program: demos
 * @description:
 * @author: 刘振华
 * @create: 2020-06-11 19:00
 **/
public class QuickUnion extends UnionFind {
	public QuickUnion(int capacity){
		super(capacity);
	}
	@Override
	public int union(int v1, int v2) {
		int oldRoot = find(v1);
		int newRoot = find(v2);
		if(oldRoot == newRoot){
			return oldRoot;
		}
		parent[oldRoot] = newRoot;
		return newRoot;
	}

	@Override
	public int find(int v) {
		rangeCheck(v);
		int i = v;
		while (i != parent[i]){
			i = parent[i];
		}
		return i;
	}
}
